<?php
    include_once('controller/product/detail.php');
    class block_quickViewBlock_controller extends controllerBase
    {
        public function process()
        {
            core::checkSession();
            $temp = explode('/',$this->get('uri'));
            $this->iId = end($temp);
            $oDetailController = new product_detail_Controller();
            $oDetailController->iId = $this->iId;

            if( !isset($_COOKIE[$oDetailController->iId]))
            {
                $oDetailController->updateProductTotalView();
                setcookie($oDetailController->iId, 'data', time()+(int)$oDetailController->getTimeView(),'/');
            }

            $uri = $this->get('uri');
            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));
            $graph->load($uri); 
            $this->aParams['name'] = $graph->resource($uri)->getString('bus:name');
            $this->aParams['images'] = $graph->resource($uri)->getList('bus:image');
            $this->aParams['publishTime'] = $graph->resource($uri)->getString('bus:publishTime');
            $this->aParams['location'] = $graph->resource($uri)->getString('bus:location');
            $this->aParams['price'] = $graph->resource($uri)->getString('bus:price');
            $this->aParams['hasCategory'] = $graph->resource($uri)->getString('bus:hasCategory');
            $this->aParams['hasManufacturer'] = $graph->resource($uri)->getString('bus:hasManufacturer');
            $this->aParams['ownUser'] = $graph->resource($uri)->getString('bus:ownUser');
            $this->aParams['description'] = $graph->resource($uri)->getString('bus:description');

            // Check if current USer like this product            
            $totalLike = 0;
            if($graph->resource($uri)->has('bus:userLike'))
            {
                $UserLikes = $graph->resource($uri)->getList('bus:userLike');
                $totalLike = count($UserLikes);
                if(isset($_SESSION['user_uri']))
                {
                    $isLiked = array_search($_SESSION['user_uri'],$UserLikes);
                    if($isLiked !== false)
                    {
                        $this->aParams['isLiked'] = true;
                    }
                }
            }
            $this->aParams['totalLike'] = $totalLike;
            // Get CategoryName
            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));
            $uri = $this->aParams['hasCategory'];
            $graph->load($uri); 
            $this->aParams['categoryName'] = $graph->resource($uri)->getString('bus:name');
            // Get total View
            $otherUri = URL::getLink('id/'.$this->iId.'_other');
            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));    
            $graph->load($otherUri);  
            $this->aParams['totalView'] = $graph->resource($otherUri)->get('bus:totalView');
            // Get CategoryName
            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));
            $uri = $this->aParams['hasManufacturer'];
            $graph->load($uri); 
            $this->aParams['manufacturerName'] = $graph->resource($uri)->getString('bus:name');
            // Get User name
            $graph = new Graphite();
            $graph->ns('bus',URL::getLink('id/bussiness#'));
            $uri = $this->aParams['ownUser'];
            $graph->load($uri); 
            $this->aParams['ownUserName'] = $graph->resource($uri)->getString('foaf:familyName');
            if($graph->resource($uri)->has('bus:image'))
            {
                $this->aParams['ownUserImage'] = $graph->resource($uri)->getString('bus:image');
            }
            $this->aParams['givenName'] = $graph->resource($uri)->getString('foaf:givenName');

            // Check if product of current User
            if(isset($_SESSION['user_uri']))
            {
                if($uri == $_SESSION['user_uri'])
                {
                    $this->aParams['isOwnUser'] = true;
                }
            }
        }
    }
?>
